package com.easypan.utils;

import com.easypan.entity.constants.Constants;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;

//生成随机数
public class StringTools {
    public static final String getRandomNumber(Integer count){
        return RandomStringUtils.random(count,false,true);
    }

    public static final String getRandomString(Integer count){
        return RandomStringUtils.random(count,true,true);
    }

    public static boolean isEmpty(String str) {
        if (null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)) {
            return true;
        } else if ("".equals(str.trim())) {
            return true;
        }
        return false;
    }

    public static String encodeByMd5(String orignString){
        return isEmpty(orignString) ? null: DigestUtils.md5Hex(orignString);
    }

    public static boolean pathIsOk(String path) {
        if (StringTools.isEmpty(path)) {
            return true;
        }
        if (path.contains("../") || path.contains("..\\")) {
            return false;
        }
        return true;
    }

    //自动生成随机数重命名文件
    public static String rename(String fileName){
        String fileNameReal=getFileNameNoSuffix(fileName);//文件名
        String suffix=getFileSuffix(fileName);//文件后缀名
        return fileNameReal+"_"+getRandomString(Constants.LENGTH_5)+suffix;//拼接后组成新的文件名
    }

    //拿文件名
    public static String getFileNameNoSuffix(String fileName){
        Integer index = fileName.lastIndexOf(".");
        if(index == -1){
            return fileName;
        }
        fileName=fileName.substring(0,index);
        return fileName;
    }

    //拿文件后缀名
    public static String getFileSuffix(String fileName){
        Integer index =fileName.lastIndexOf(".");
        if(index == -1){
            return "";
        }
        String suffix = fileName.substring(index);
        return suffix;
    }
}
